<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
             :close-on-press-escape='false' :fullscreen='true'>
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">

                <el-form-item label="交易号" prop="tradekey">
            <el-input v-model="dataForm.tradekey" placeholder="交易号"></el-input>
          </el-form-item>
          <el-form-item label="交易类型" prop="tradetype">
            <el-input v-model="dataForm.tradetype" placeholder="交易类型"></el-input>
          </el-form-item>
          <el-form-item label="物料" prop="sku">
            <el-input v-model="dataForm.sku" placeholder="物料"></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="qty">
            <el-input v-model="dataForm.qty" placeholder="数量"></el-input>
          </el-form-item>
          <el-form-item label="来源单类型" prop="sourcetype">
            <el-input v-model="dataForm.sourcetype" placeholder="来源单类型"></el-input>
          </el-form-item>
          <el-form-item label="来源单号" prop="sourcekey">
            <el-input v-model="dataForm.sourcekey" placeholder="来源单号"></el-input>
          </el-form-item>
          <el-form-item label="来源单行号" prop="sourceline">
            <el-input v-model="dataForm.sourceline" placeholder="来源单行号"></el-input>
          </el-form-item>
          <el-form-item label="来源单记录行号" prop="sourcelinelist">
            <el-input v-model="dataForm.sourcelinelist" placeholder="来源单记录行号"></el-input>
          </el-form-item>
          <el-form-item label="自库位" prop="fromlocation">
            <el-input v-model="dataForm.fromlocation" placeholder="自库位"></el-input>
          </el-form-item>
          <el-form-item label="至库位" prop="tolocation">
            <el-input v-model="dataForm.tolocation" placeholder="至库位"></el-input>
          </el-form-item>
          <el-form-item label="自箱" prop="fromtray">
            <el-input v-model="dataForm.fromtray" placeholder="自箱"></el-input>
          </el-form-item>
          <el-form-item label="至箱" prop="totray">
            <el-input v-model="dataForm.totray" placeholder="至箱"></el-input>
          </el-form-item>
          <el-form-item label="自格" prop="frombox">
            <el-input v-model="dataForm.frombox" placeholder="自格"></el-input>
          </el-form-item>
          <el-form-item label="至格" prop="tobox">
            <el-input v-model="dataForm.tobox" placeholder="至格"></el-input>
          </el-form-item>
          <el-form-item label="自生产日期" prop="fromlot1">
            <el-input v-model="dataForm.fromlot1" placeholder="自生产日期"></el-input>
          </el-form-item>
          <el-form-item label="自有效日期" prop="fromlot2">
            <el-input v-model="dataForm.fromlot2" placeholder="自有效日期"></el-input>
          </el-form-item>
          <el-form-item label="自属性3" prop="fromlot3">
            <el-input v-model="dataForm.fromlot3" placeholder="自属性3"></el-input>
          </el-form-item>
          <el-form-item label="自属性4" prop="fromlot4">
            <el-input v-model="dataForm.fromlot4" placeholder="自属性4"></el-input>
          </el-form-item>
          <el-form-item label="自属性5" prop="fromlot5">
            <el-input v-model="dataForm.fromlot5" placeholder="自属性5"></el-input>
          </el-form-item>
          <el-form-item label="自属性6" prop="fromlot6">
            <el-input v-model="dataForm.fromlot6" placeholder="自属性6"></el-input>
          </el-form-item>
          <el-form-item label="自属性7" prop="fromlot7">
            <el-input v-model="dataForm.fromlot7" placeholder="自属性7"></el-input>
          </el-form-item>
          <el-form-item label="自属性8" prop="fromlot8">
            <el-input v-model="dataForm.fromlot8" placeholder="自属性8"></el-input>
          </el-form-item>
          <el-form-item label="自属性9" prop="fromlot9">
            <el-input v-model="dataForm.fromlot9" placeholder="自属性9"></el-input>
          </el-form-item>
          <el-form-item label="自属性10" prop="fromlot10">
            <el-input v-model="dataForm.fromlot10" placeholder="自属性10"></el-input>
          </el-form-item>
          <el-form-item label="至生产日期" prop="tolot1">
            <el-input v-model="dataForm.tolot1" placeholder="至生产日期"></el-input>
          </el-form-item>
          <el-form-item label="至有效日期" prop="tolot2">
            <el-input v-model="dataForm.tolot2" placeholder="至有效日期"></el-input>
          </el-form-item>
          <el-form-item label="至属性3" prop="tolot3">
            <el-input v-model="dataForm.tolot3" placeholder="至属性3"></el-input>
          </el-form-item>
          <el-form-item label="至属性4" prop="tolot4">
            <el-input v-model="dataForm.tolot4" placeholder="至属性4"></el-input>
          </el-form-item>
          <el-form-item label="至属性5" prop="tolot5">
            <el-input v-model="dataForm.tolot5" placeholder="至属性5"></el-input>
          </el-form-item>
          <el-form-item label="至属性6" prop="tolot6">
            <el-input v-model="dataForm.tolot6" placeholder="至属性6"></el-input>
          </el-form-item>
          <el-form-item label="至属性7" prop="tolot7">
            <el-input v-model="dataForm.tolot7" placeholder="至属性7"></el-input>
          </el-form-item>
          <el-form-item label="至属性8" prop="tolot8">
            <el-input v-model="dataForm.tolot8" placeholder="至属性8"></el-input>
          </el-form-item>
          <el-form-item label="至属性9" prop="tolot9">
            <el-input v-model="dataForm.tolot9" placeholder="至属性9"></el-input>
          </el-form-item>
          <el-form-item label="至属性10" prop="tolot10">
            <el-input v-model="dataForm.tolot10" placeholder="至属性10"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-input v-model="dataForm.status" placeholder="状态"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {ref, reactive, onMounted, nextTick, computed} from 'vue';
import {getTradeById,putTrade,postTrade} from "@/network/lswms/stock/Trade"
import {ElMessage} from 'element-plus'
import _ from 'lodash'


export default {
  name: "tradeAddOrUpdate",
  emits: ["refreshDataList"],
  setup(props, {emit}) {
    const visible = ref(false);
    const dataForm = ref({
                          id: '',
          tradekey: '',
          tradetype: '',
          sku: '',
          qty: '',
          sourcetype: '',
          sourcekey: '',
          sourceline: '',
          sourcelinelist: '',
		  fromlot: '',
		  tolot: '',
          fromlocation: '',
          tolocation: '',
          fromtray: '',
          totray: '',
          frombox: '',
          tobox: '',
          fromlot1: '',
          fromlot2: '',
          fromlot3: '',
          fromlot4: '',
          fromlot5: '',
          fromlot6: '',
          fromlot7: '',
          fromlot8: '',
          fromlot9: '',
          fromlot10: '',
          tolot1: '',
          tolot2: '',
          tolot3: '',
          tolot4: '',
          tolot5: '',
          tolot6: '',
          tolot7: '',
          tolot8: '',
          tolot9: '',
          tolot10: '',
          status: '',
          remark: '',
          adduser: '',
          addtime: '',
                  updatuser: '',
                  updatetime: ''
    });


    const dataRule = reactive({
                tradekey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tradetype: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sku: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          qty: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sourcetype: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sourcekey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sourceline: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sourcelinelist: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlocation: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolocation: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromtray: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          totray: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          frombox: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tobox: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot1: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot2: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot3: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot4: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot5: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot6: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot7: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot8: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot9: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot10: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot1: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot2: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot3: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot4: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot5: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot6: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot7: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot8: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot9: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot10: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          status: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          remark: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],    })

    const dataFormRef = ref(null)

    const init = () => {
      visible.value = true;
      nextTick(function () {
        dataFormRef.value.resetFields();
        if (dataForm.value.id) {
                gettradeInfo();
                }

      })
    }

        const gettradeInfo =()=>{
                getTradeById(dataForm.value.id).then(function (res) {
                  if (res.code !== 0) {
                    return ElMessage.error(res.msg);
                  }
                  dataForm.value = _.assignIn({}, dataForm.value, res.data);
                }).catch((err) =>{
                });
        }

    // 表单提交
    const dataFormSubmitHandle = _.debounce(() => {
      dataFormRef.value.validate(function (valid) {
        if (!valid) {
          return false;
        }

        let qing = ()=> {
          return !dataForm.value.id ?  postTrade(dataForm.value) : putTrade(dataForm.value)
        }
        qing().then(function (res) {
          if (res.code !== 0) {
            return ElMessage.error(res.msg);
          }
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 500,
            onClose: function () {
              visible.value = false
              emit('refreshDataList')
            }
          })
        }).catch(function () {
        });

      })
    }, 1000, {'leading': true, 'trailing': false})

    return {
      visible,
      dataForm,
      dataRule,
      init,
      dataFormSubmitHandle,
      dataFormRef

    }
  },


}
</script>

<style scoped>

</style>
